<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    
<%@page import="constants.StringConstants" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function validate(){
	var name = document.getElementById("name").value;
	var dayNum = document.getElementById("dayNum").value;
	var mealNum = document.getElementById("mealNum").value;
	var calo = document.getElementById("calo").value;
	var errorSpan = document.getElementById("error");
	var regex = /^\d+$/;
	
	if (name=="" || dayNum=="" || mealNum=="" || calo=="") {
		errorSpan.innerHTML = "<strong><font color='red'>Please input all required information!!</font></strong>";
		errorSpan.style.visibility = 'visible';
		return false;
	}
	
	if (regex.test(dayNum)){
		if (dayNum < 1 || dayNum > 100) {
			errorSpan.innerHTML = "<strong><font color='red'>Day number out of range</font></strong>";
			errorSpan.style.visibility = 'visible';
			return false;
		}
	} else {
		errorSpan.innerHTML = "<strong><font color='red'>Day is number !!</font></strong>";
		errorSpan.style.visibility = 'visible';
		return false;
	}
	
	if (regex.test(mealNum)){
		if (mealNum < 1 || mealNum > 10) {
			errorSpan.innerHTML = "<strong><font color='red'>Meal number out of range</font></strong>";
			errorSpan.style.visibility = 'visible';
			return false;
		}
	} else {
		errorSpan.innerHTML = "<strong><font color='red'>Meal is number !!</font></strong>";
		errorSpan.style.visibility = 'visible';
		return false;
	}	
	
	if (regex.test(calo)){
		if (calo < 300 || calo > 3000) {
			errorSpan.innerHTML = "<strong><font color='red'>Calo out of range</font></strong>";
			errorSpan.style.visibility = 'visible';
			return false;
		}
	} else {
		errorSpan.innerHTML = "<strong><font color='red'>Calo is number !!</font></strong>";
		errorSpan.style.visibility = 'visible';
		return false;
	}	
	
	return false;
}
</script>
<title>Update Menu</title>
</head>
<body>
	<span style="float:left"><a href="/XMLProject/admin/admin-home.jsp">Go to Home</a></span><br/>
	<span style="float:right">
		Hi, <strong>${sessionScope.ADMIN_USERNAME}</strong>-
		<a href="AccountServlet?action=<%=StringConstants.LOGOUT%>">Logout</a>
	</span>
	<h2>Update Menu</h2>
	<c:set var="menuDTO" value="${requestScope.MENUDTO}"/>
	<form action="MenuMaintainServlet" method="post" enctype="multipart/form-data"  onsubmit="return validate()">
		<input type="hidden" name="id" value="${menuDTO.id}"/>
		<input type="hidden" name="oldImg" value="${menuDTO.imgPath}"/>
	<table>
	<tr>
		<td>Menu Title*</td> <td><input type="text" id="name" name="name" value="${menuDTO.name}"/></td>
	</tr>
	<tr><td>	
		Diet:
	</td>
	<td>
		<c:set var="selectedDiet" value="${menuDTO.diet.id}"/>
			<select name="dietId">
				<c:forEach var="diet" items="${requestScope.DIETLIST}"> 
					<c:if test="${diet.id eq selectedDiet}">
						<option value="${diet.id}" selected>${diet.name}</option>
					</c:if>
					<c:if test="${diet.id ne selectedDiet}">
						<option value="${diet.id}">${diet.name}</option>
					</c:if>
				</c:forEach>
			</select>
		</td>
	</tr>
	<tr>
		<td>Number of days*</td> <td><input type="text" id="dayNum" name="numberday" value="${menuDTO.numberday}"/>(range value is 1 to 100)</td>
	</tr>
	<tr>
		<td>Number of Meals per Day*</td> <td><input id="mealNum" type="text" name="numbermeal" value="${menuDTO.numbermeal}"/>(range value is 1 to 10)</td>
	</tr>
	<tr>
		<td>Calo*</td> <td><input type="text" id="calo" name="calo" value=""/>(range value is 300 to 3000)</td>
	</tr>
	</table>
		Description: <textarea name="description" rows="10" cols="100">${menuDTO.description}</textarea><br/>
		Image: <img src="${menuDTO.imgPath}" width="30px" height="30px"/>
		<input type="file" name="img" value=""/><br/>
		<a href="PlanServlet?action=<%=StringConstants.LOAD_MENU_MEALS%>&menuId=${menuDTO.id}&numberOfDays=${menuDTO.numberday}&numberMealsPerDay=${menuDTO.numbermeal}">Configure Plan of Menu</a>
		<br/>
		<br/>
		<input type="submit" name="action" value="edit"><br/>
		<span id="error" style="visibility: hidden;"></span>
	</form>
</body>
</html>